package com.regentsoft.cloudback.operateAdmin.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.regentsoft.cloudback.base.BaseResp;
import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.dao.CompanyApplicationMapper;
import com.regentsoft.cloudback.operateAdmin.entity.CompanyApplication;
import com.regentsoft.cloudback.operateAdmin.service.CompanyApplicationService;

/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Service
public class CompanyApplicationServiceImpl  implements CompanyApplicationService {
	@Autowired
	private CompanyApplicationMapper mapper;
	@Override
	public CompanyApplication findById(String pk) throws Exception {
		return mapper.findById(pk);
	}

	@Override
	public List<CompanyApplication> findList(CompanyApplication obj) throws Exception {
		return mapper.findList(obj);
	}

	@Override
	public Pager findPageList(CompanyApplication obj, Integer pageNumber, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int add(CompanyApplication obj) throws Exception {
		return mapper.add(obj);
	}

	@Override
	public int update(CompanyApplication obj) throws Exception {
		return mapper.update(obj);
	}

	@Override
	public int delete(String pk) throws Exception {
		return mapper.deleteById(pk);
	}

	@Override
	public int getTotalCount(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<CompanyApplication> queryByPage(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional
	public BaseResp editCompanyApplication(String appId,String companyId, String link) throws Exception{
		CompanyApplication ca = new CompanyApplication();
		BaseResp resp = new BaseResp();
		//根据companyId查找公司
		CompanyApplication flag = mapper.findByCompanyId(companyId);
		ca.setCompanyId(companyId);
		ca.setAppId(appId);
		ca.setLink(link);
		try {
			if(flag!=null) {
				//update
				mapper.update(ca);
			}else {
				//add
				ca.setId(UUID.randomUUID().toString());
				mapper.add(ca);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		resp.setCode(ResponseCode.SUCCESS);
		return resp;
	}

}
